home *** CD-ROM | disk | FTP | other *** search
- Path: news.unt.edu!news
- From: Steve Fogoros <sfogoros@hsc.unt.edu>
- Newsgroups: comp.lang.c
- Subject: Re: Rand() Function
- Date: Thu, 21 Mar 1996 09:01:27 -0800
- Organization: University of North Texas Health Science Center
- Message-ID: <31518B67.3914@hsc.unt.edu>
- References: <4iqltj$40g@news1.sympatico.ca>
- NNTP-Posting-Host: sfogoros.hsc.unt.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Gisele Swinson wrote:
- >
- > How do I get a random number from a "SET" of numbers.
- >
- > e.g.
- >
- > I want a random number from this set:
- >
- > 2, 4, 6, 8, 10
- >
- > It seems so simple but I can't find anything in my text how to do this.
- >
- > Any help would be appreciated.
- >
- > Thanks
- >
- > Gisele
- >
- > gisele.swinson@sympatico.ca
-
- Try:
-
-
- #include <stdio.h>
-
- int set[] = { 2,4,6,8,10 };
-
- main()
- {
- int i, j;
-
- for(j=0;j<100;j++)
- {
- i = rand() % (sizeof(set) / sizeof(int));
- printf("%d ",set[i]);
- }
- }
-
-
- --
- Steve Fogoros, Academic Information Coordinator
- University of North Texas Health Science Center
- sfogoros@hsc.unt.edu
-